<template>
	<view class="show-list-main">
		<view class="mt-post-content">
			<view class="post-left" v-if="leftPosts.length > 0">
				<view class="post-item" v-for="(item,index) in leftPosts" :key="index">
					<view class="post-image">
						<image :src="item.url" mode="aspectFill" v-if="!item.isVedio" @click="clickPostItem(item)">
						</image>
						<!-- <video :src="item.url" controls preload="metadata" crossorigin="anonymous"
							:poster="item.videoCover ? item.videoCover : ''" v-else>
						</video> -->
						<!-- 由于uniapp组件video层级太高，所以用自定义的视频组件 -->
						<DomVideoPlayer class="DomVideoPlayer" :src="item.url" :autoplay="false" controls muted
							:isLoading="true" v-else @click="clickPostItem(item)">
						</DomVideoPlayer>
						<view class="radio-view" v-show="ManageMode">
							<label class="radio">
								<radio :checked="item.isChecked" @click="clickRadio(item)" color="#66596b" />
							</label>
						</view>
					</view>
					<view class="post-name" @click="clickPostItem(item)">
						{{item.title}}
					</view>
					<view class="post-time" @click="clickPostItem(item)">
						{{item.releaseTime}}
					</view>
					<view class="post-like-num-comment" @click="clickPostItem(item)">
						<view class="likeicon-num">
							<view class="post-like">
								<uni-icons type="heart" color="#7a7a7a" v-if="!item.isLike"></uni-icons>
								<uni-icons type="heart-filled" color="red" v-else></uni-icons>
							</view>
							<view class="like-num">
								{{item.likeCount}}
							</view>
						</view>
						<view class="comment-num-view">
							<view class="comment-icon">
								<uni-icons type="chat" color="#7a7a7a"></uni-icons>
							</view>
							<view class="comment-num">
								{{item.commentCount}}
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="post-right" v-if="rightPosts.length > 0">
				<view class="post-item" v-for="(item,index) in rightPosts" :key="index">
					<view class="post-image">
						<image :src="item.url" mode="aspectFill" v-if="!item.isVedio" @click="clickPostItem(item)">
						</image>
						<!-- <video :src="item.url" controls preload="metadata" crossorigin="anonymous"
							:poster="item.videoCover ? item.videoCover : ''" v-else></video> -->
						<DomVideoPlayer class="DomVideoPlayer" :src="item.url" :autoplay="false" controls muted
							:isLoading="true" v-else @click="clickPostItem(item)">
						</DomVideoPlayer>
						<view class="radio-view" v-show="ManageMode">
							<label class="radio">
								<radio :checked="item.isChecked" @click="clickRadio(item)" color="#66596b" />
							</label>
						</view>
					</view>
					<view class="post-name" @click="clickPostItem(item)">
						{{item.title}}
					</view>
					<view class="post-time" @click="clickPostItem(item)">
						{{item.releaseTime}}
					</view>
					<view class="post-like-num-comment" @click="clickPostItem(item)">
						<view class="likeicon-num">
							<view class="post-like">
								<uni-icons type="heart" color="#7a7a7a" v-if="!item.isLike"></uni-icons>
								<uni-icons type="heart-filled" color="red" v-else></uni-icons>
							</view>
							<view class="like-num">
								{{item.likeCount}}
							</view>
						</view>
						<view class="comment-num-view">
							<view class="comment-icon">
								<uni-icons type="chat" color="#7a7a7a"></uni-icons>
							</view>
							<view class="comment-num">
								{{item.commentCount}}
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import mitter from '@/common/mitt/eventBus.js'
	export default {
		props: {
			// 定义接收父组件传递的infos数组，这里要求数组元素是包含url属性的对象
			notes: {
				type: Array,
				default: () => []
			},
			ManageMode: {
				type: Boolean,
				default: false
			},
			selectAll: {
				type: Boolean,
				default: false
			},
		},
		data() {
			return {
				leftPosts: [],
				rightPosts: [],
				newNotes: [],
				deleteNoteAids: [],
			};
		},

		watch: {
			// 监听newNotes数组中每个元素的isChecked属性变化
			// 'newNotes.[].isChecked': {
			// 	handler(newValue, oldValue) {
			// 		// 每当isChecked属性变化时，重新整理deleteNoteAids数组
			// 		this.deleteNoteAids = [];
			// 		this.newNotes.forEach(note => {
			// 			if (note.isChecked) {
			// 				this.deleteNoteAids.push(note.noteAid);
			// 			}
			// 		});
			// 	},
			// 	deep: true // 需要设置deep为true，才能深度监听数组中对象属性的变化
			// },
			// 新增：监听父组件传递过来的notes数据变化
			notes: {
				handler(newValue) {
					// 当notes数据变化时，更新this.newNotes
					this.newNotes = newValue;

					// 重新划分leftPosts和rightPosts
					this.dividerPosts();
				},
				immediate: true // 立即执行一次，确保初始数据也能进行划分处理
			},
			selectAll: {
				handler(newValue, oldValue) {
					if (this.selectAll) {
						this.deleteNoteAids = [];
						this.newNotes.forEach(item => {
							item.isChecked = true;
							this.deleteNoteAids.push(item.noteAid)
						})
						this.leftPosts.forEach(item => {
							item.isChecked = true;
						})
						this.rightPosts.forEach(item => {
							item.isChecked = true;
						})
					}
					// 触发自定义事件，将最新的deleteNoteAids数据传递给父组件
					this.$emit('update-delete-note-aids', this.deleteNoteAids);
				},
				deep: true
			},
		},

		mounted() {
			this.newNotes = this.notes;
			this.dividerPosts();
			// 添加searchUserNoteResult事件监听，只在这里添加一次，避免重复添加
			mitter.on('searchUserNoteResult', (data) => {
				this.newNotes = data;
				this.dividerPosts();
			})

		},
		beforeDestroy() {
			mitter.off('searchNoteOrCollectResult');
		},
		onHide() {
			mitter.off('searchNoteOrCollectResult');
		},

		methods: {

			dividerPosts() {
				this.leftPosts = [];
				this.rightPosts = [];
				this.newNotes.forEach(item => {
					item.isChecked = false;
				})
				for (var i = 0; i < this.newNotes.length; i++) {
					if (i % 2 === 0) {
						this.leftPosts.push(this.newNotes[i])
					} else {
						this.rightPosts.push(this.newNotes[i])
					}
				}
			},
			clickPostItem(item) {
				uni.navigateTo({
					url: `/pages/tabBar/component/topBar/travel/noteDetailPage?aid=${item.noteAid}`
				})
			},
			clickRadio(item) {
				if (item.isChecked) {
					item.isChecked = false;
					// 当取消选中（isChecked变为false）时，查找数组中是否存在对应aid，若存在则移除
					const index = this.deleteNoteAids.findIndex(aid => aid === item.noteAid);
					if (index !== -1) {
						this.deleteNoteAids.splice(index, 1);
					}
					this.$emit('update-all-select', false)
					console.log(this.deleteNoteAids);
					// 触发自定义事件，将最新的deleteNoteAids数据传递给父组件
					this.$emit('update-delete-note-aids', this.deleteNoteAids);
				} else {
					item.isChecked = true;
					this.deleteNoteAids = [];
					this.newNotes.forEach(item=>{
						if(item.isChecked){
							this.deleteNoteAids.push(item.noteAid)
						}
					})
					this.$emit('update-all-select', this.newNotes.length === this.deleteNoteAids.length)
					// 触发自定义事件，将最新的deleteNoteAids数据传递给父组件
					this.$emit('update-delete-note-aids', this.deleteNoteAids);
				}
			}
		},
	};
</script>

<style lang="scss" scoped>
	.show-list-main {

		.mt-post-content {
			width: 100%;
			margin-left: auto;
			margin-right: auto;
			margin-top: 60rpx;
			display: flex;
			justify-content: space-between;

			.post-left {
				width: 49%;
			}

			.post-right {
				width: 49%;
			}

			.post-item {
				width: 100%;
				box-shadow: 3px 3px 5px rgba(0, 0, 0, 0.3);
				margin-bottom: 30rpx;

				.post-image {
					// z-index: 1;
					position: relative;

					.radio-view {
						width: 70rpx;
						height: 70rpx;
						display: flex;
						align-items: center;
						justify-content: center;
						position: absolute;
						top: 0;
						right: 0;
						z-index: 1;
					}

					image {
						width: 100%;
						height: 300rpx;
					}

					.DomVideoPlayer {
						width: 100%;
						height: 300rpx;
					}

					.video-cover-content {
						position: absolute;
						top: 0;
						left: 0;
						width: 100%;
						height: 100%;
						display: flex;
						flex-direction: column;
						justify-content: center;
						align-items: center;
						background-color: rgba(0, 0, 0, 0.5);
					}

					.cover-text {
						font-size: 24rpx;
						color: #fff;
						margin-bottom: 10rpx;
					}

					.cover-icon {
						color: #fff;
					}

					video {
						width: 100%;
						height: 300rpx;
						background-size: cover;
					}
				}

				.post-name {
					padding-top: 20rpx;
					padding-left: 20rpx;
					padding-right: 20rpx;
					font-size: 28rpx;
				}

				.post-time {
					margin-top: 20rpx;
					padding-left: 20rpx;
					padding-right: 20rpx;
					font-size: 24rpx;
					color: #6c6c6c;
				}

				.post-like-num-comment {
					margin-top: 20rpx;
					padding-left: 20rpx;
					padding-right: 20rpx;
					padding-bottom: 20rpx;
					display: flex;
					flex-direction: row;
					justify-content: space-between;

					.likeicon-num {
						display: flex;
						justify-content: center;
						align-items: center;

						.like-num {
							margin-left: 8rpx;
							font-size: 25rpx;
							color: #6c6c6c;
						}
					}

					.comment-num-view {
						display: flex;
						align-items: center;

						.comment-num {
							margin-left: 8rpx;
							font-size: 25rpx;
							color: #6c6c6c;
						}
					}
				}
			}
		}

	}
</style>